package com.spring.until;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SMSUtil {
    public static String code1=null;
    public static long time1;
    // 短信应用SDK AppID  1400开头
    private static int appid =  1400587498;
    // 短信应用SDK AppKey
    private static String appkey = "136c3e78c5ed4bdb32492818bb22d759";
    // 短信模板ID，需要在短信应用中申请
    private static int templateId = 1173986 ;
    // 签名，使用的是签名内容，而不是签名ID
    private static String smsSign = "Sineb公众号";

    public static boolean sendSMS(String tel ,String code) {

        code1=code;
        try {
            //参数，一定要对应短信模板中的参数顺序和个数，
            String[] params = {code};
            //创建ssender对象
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            //发送
            SmsSingleSenderResult result = ssender.sendWithParam("86", tel,templateId, params, smsSign, "", "");
            if(result.errMsg.equals("OK")){
                return true ;
            }
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }catch (Exception e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return false;
    }

}

